package activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author lishaojun
 * @create 2019-07-10-下午 6:48
 * 持久化的topic 消费者先订阅，掉线，下次上线，数据每收到的，补充收到，类似于短信
 */
public class JmsProduce_Topic_Persist {
    public static final String ACTIVEMQ_URL = "tcp://106.13.85.181:61616";
    public static final String TOPIC_NAME = "topic-persist";

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂,按照地址给定的用户名密码
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2.通过连接工厂获得连接
        Connection connection = activeMQConnectionFactory.createConnection();

        //3.创建会话,方法两个参数一个是事务，一个是签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地，具体是队列或者是主题
        Topic topic = session.createTopic(TOPIC_NAME);
        //5.创建消息的生产者

        MessageProducer messageProducer = session.createProducer(topic);
        //持久化
        messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();
        //6.通过使用消息生产者生产三条消息发送到mq的队列里
        for (int i = 1; i <= 3; i++) {
            //7.创建消息
            TextMessage textMessage = session.createTextMessage("topic----" + i);
            //8.通过消息生产者发布
            messageProducer.send(textMessage);
        }
        //9.关闭资源
        messageProducer.close();
        session.close();
        connection.close();
        System.out.println("主题生产完毕");
    }

}
